package com.example.user

import android.app.Application
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.witmed.lib_base.BaseEntity
import com.witmed.lib_base.BaseLiveData
import com.witmed.lib_base.BaseOBViewModel
import com.witmed.lib_base.BaseViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.RequestBody

class LoginViewModel() :BaseOBViewModel<LoginModel>(){
     var listener: LoginListener?=null
    lateinit var entity: UserEntity
    override fun createModel(): LoginModel {
        return LoginModel()
    }

    override fun initData() {

    }

    override fun onNext(entity: BaseEntity) {
        val loginEntity = entity as LoginEntity
        Log.i("===",loginEntity.getLoginValues()?.token!!)

    }

    fun getMyEntity():UserEntity{
        if(!this::entity.isInitialized){
            entity= UserEntity()
        }
        return entity
    }

    fun getMyListener():LoginListener{
        if(listener==null){
            listener= LoginListener()
        }
        return listener!!
    }

     inner class LoginListener {

        fun login(){
//            ToastUtils.showLong("登录")
            val map = hashMapOf(
                Pair("uName", entity.getUserName()),
                Pair("uPwd", entity.getUserPwd()),
                Pair("time", (System.currentTimeMillis() / 1000).toString())
            )
            val json = GsonUtils.toJson(map)
            val body = RequestBody.create(MediaType.parse("application/json"), json)
            m.request(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this@LoginViewModel)
        }

        fun toRegister(){
            ARouter.getInstance().build("/user/RegisterActivity").navigation()
        }
    }
}


